/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
 var findPairs = function(nums, k) {

  nums.sort((a, b) => a - b);
  let l = 0, r = 1;
  let ret = 0;
  while(r < nums.length && l < nums.length) {
    console.log(l, r);
      while(nums[r] < nums[l] + k) {
          r++;
      }
      if(nums[r] === nums[l] + k) {
          ret++;
      }
      const t = nums[l];
      while(nums[l] === t) {
          l++;
      }
      r = Math.max(l + 1, r);
  }
  return ret;
};

console.log(findPairs([1, 3, 1, 5, 4], 0));